package com.ccp.dev.system.service;
import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.system.dao.MessageReadDao;
import com.ccp.dev.system.model.MessageRead;
import com.ccp.dev.system.model.SysUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
 * 对象功能:接收状态 Service类
 * 开发人员:csx
 * 创建时间:2012-01-14 15:14:52
 */
@Service
public class MessageReadService extends BaseService<MessageRead>
{
	@Resource
	private MessageReadDao dao;

	/**
	 * 添加数据MessageRead
	 * @param messageId 消息主键
	 * @param sysUser 当前用户信息
	 */
	@Transactional(rollbackFor = Exception.class)
	public void addMessageRead(String messageId, SysUser sysUser){
		MessageRead msgRead = dao.getReadByUser(messageId,sysUser.getUserId());
		if(msgRead==null){
			Date now = new Date();
			MessageRead messageRead = new MessageRead();
			messageRead.setId(UUIDUtils.getUUIDFor32());
			messageRead.setMessageId(messageId);
			messageRead.setReceiverId(sysUser.getUserId());
			messageRead.setReceiver(sysUser.getFullName());
			messageRead.setReceiveTime(now);
			add(messageRead);
		}
	}
	
	/**
	 * 获得已读此消息的人员
	 * @param messageId
	 * @return List<MessageRead>
	 */
	public List<MessageRead> getReadByMsgId(String messageId) {
		return dao.getReadByMsgId(messageId);
	}

	/** 获取信息已读
	 *
	 * @param id
	 * @return MessageRead
	 */
	public MessageRead getById(String id) {
	return 	dao.getById(id);
	}
}
